package com.gs.SynAntiWandering.fragment;

import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.gs.SynAntiWandering.R;
import com.gs.SynAntiWandering.activity.insight.InsightInOutActivity;
import com.gs.SynAntiWandering.adapter.InsightAdapter;
import com.gs.SynAntiWandering.apiresult.ApiResultData;
import com.gs.SynAntiWandering.constant.AllConstants;
import com.gs.SynAntiWandering.db.dto.InsightAlertDto;
import com.gs.SynAntiWandering.db.dto.InsightDto;
import com.gs.SynAntiWandering.db.query.BaseQuery;
import com.gs.SynAntiWandering.fragment.base.BaseListFragment;
import com.gs.SynAntiWandering.presenter.InsightPresenter;
import com.gs.SynAntiWandering.view.IInsightView;

public class InsightFragment extends BaseListFragment<IInsightView, InsightPresenter, InsightAlertDto, BaseQuery> implements IInsightView {

    private InsightAdapter adapter;
    private boolean isShow = true;
    private final boolean isThread = true;
    private static Thread thread;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_insight;
    }

    @Override
    protected InsightPresenter createPresenter() {
        return new InsightPresenter();
    }

    @Override
    protected BaseQuery createQuery() {
        return new BaseQuery();
    }

    @Override
    protected void initView() {
        smartRefreshLayout.setEnableLoadMore(false);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setItemAnimator(null);
        adapter = new InsightAdapter(context, mData, R.layout.rvitem_insight);
        adapter.setOnItemChildrenClickListener((v, position) -> {
            isShow = false;
            Intent intent = new Intent();
            intent.setClass(context, InsightInOutActivity.class);
            intent.putExtra(AllConstants.ID, mData.get(position).getSerialNo());
            startActivityForResult(intent, 1);
        });
        recyclerView.setLayoutManager(new GridLayoutManager(context, 2));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void loadData() {
        query.init();
        mPresenter.getData();
    }

    @Override
    protected void search() {
        if (thread != null) thread.interrupt();
        thread = new Thread(() -> {
            try {
                while (isThread) {
                    if (isShow) {
                        query.init();
                        mPresenter.getData();
                    }
                    Thread.sleep(10 * 1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread.start();
    }

    @Override
    public void filter() {

    }

    @Override
    public void onGetDataSuccess(ApiResultData<InsightDto> apiResult) {
        refreshSmartRefreshLayout();
        if (apiResult.getStatus() == 2000) {
            mData.clear();
            mData.addAll(apiResult.getData().getAlarmList());
            adapter.notifyDataSetChanged();
        } else if (apiResult.getStatus() == 4001) { // token过期不再继续循环请求
            isShow = false;
        }
    }

    @Override
    public void onGetDataFails(Exception e) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (thread != null) {
            thread.interrupt();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isShow = !hidden;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            isShow = true;
        }
    }
}
